package zk;

import java.awt.Rectangle;
import java.io.IOException;
import java.util.List;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.text.PDFTextStripperByArea;
import org.springframework.core.io.ClassPathResource;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.ReUtil;
import cn.hutool.json.JSONObject;

// 自学考试专业计划解析1
public class SelfStudyExamMajorPlanParse {

	
	public static void main(String[] args) {		
		doHandleUnFixed();
	}
	
	// 处理未课改专业
	public static void doHandleUnFixed() {
		ClassPathResource rs = new ClassPathResource("湖北省高等教育自学考试专业计划汇编2018年版.pdf");
		try {
			PDDocument doc = PDDocument.load(rs.getFile());

			for (int i = 13; i <= 98; i++) {
				PDPage page = doc.getPage(i);
				handleUnFixedPage(page);
			}
			doc.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void handleUnFixedPage(PDPage page) throws IOException {
		// 创建一个PDFTextStripperByArea对象，用于提取表格区域的文本
		PDFTextStripperByArea stripper = new PDFTextStripperByArea();
		stripper.setSortByPosition(true);

		// 定义表格的区域（左上角和右下角坐标）
		Rectangle title = new Rectangle(0, 50, 800, 50);
		stripper.addRegion("title", title);

		Rectangle majorCode = new Rectangle(0, 100, 800, 50);
		stripper.addRegion("majorCode", majorCode);

		// 定义表格的区域（左上角和右下角坐标）
		Rectangle rect = new Rectangle(10, 180, 800, 1080);
		stripper.addRegion("table", rect);

		// 将PDF文档的第一页应用于PDFTextStripperByArea对象
		stripper.extractRegions(page);

		String titleData = stripper.getTextForRegion("title");
		titleData = titleData.replace("※", "").trim();
		String majorName = titleData.replace("（本科）", "").replace("（专科）", "");
		String studyMode = titleData.indexOf("本科") >= 0 ? "本科" : "专科";

		String majorCodeData = stripper.getTextForRegion("majorCode");

		int s = majorCodeData.indexOf("专业代码：");
		int e = majorCodeData.indexOf("；");

		majorCodeData = majorCodeData.substring(s + 5, e).trim();
		// 获取提取的表格数据
		String tableData = stripper.getTextForRegion("table");
		String[] lines = tableData.split("\r\n");

		List<JSONObject> rows = CollUtil.newArrayList();
		for (String line : lines) {
			if (ReUtil.isMatch(".*\\d{5}\\s+.*\\s+\\d{1,2}.*", line)) {
				List<String> list = ReUtil.findAllGroup0("\\d{5}\\s+.*\\s+\\d{1,2}", line);
				String[] arr = list.get(0).split(" ");
				
				JSONObject jsonObj = new JSONObject()//
						.set("courseCode", arr[0])//
						.set("courseName", arr[1])//
						.set("credit", arr[2])//
						.set("majorCode", majorCodeData)//
						.set("majorName", majorName)//
						.set("studyMode", studyMode)//
				;
				rows.add(jsonObj);

			} else if (ReUtil.isMatch(".*\\d{5}\\s+.*", line)) {
				List<String> list = ReUtil.findAllGroup0("\\d{5}\\s+.*", line);
				String[] arr = list.get(0).split(" ");

				JSONObject jsonObj = new JSONObject()//
						.set("courseCode", arr[0])//
						.set("courseName", arr[1])//
						.set("credit", "0")//
						.set("majorCode", majorCodeData)//
						.set("majorName", majorName)//
						.set("studyMode", studyMode)//
				;
				rows.add(jsonObj);
			}
		}
		Console.log(rows);
		// 	TODO ? 可以把 rows写入到数据库,或者写入到excel
	}

}
